package sk.naive.talker.command;

import sk.naive.talker.*;
import sk.naive.talker.message.*;
import sk.naive.talker.util.Utils;

import java.rmi.RemoteException;

/**
 * Force command.
 *
 * @author <a href="mailto:richter@bgs.sk">Richard "Virgo" Richter</a>
 * @version $Revision: 1.14 $ $Date: 2005/01/25 21:57:04 $
 */
public class Force extends AbstractCommand {
	public void exec() throws CommandException, RemoteException {
		Utils.checkPermission(user, Consts.PERM_SUPERUSER);
		String[] sa = Utils.splitWords(params, 2);
		if (sa.length < 2) {
			sendMisusageWarning();
			return;
		}
		User victim = findTarget(sa[0]);
		String performance = sa[1];
		if (victim == null) {
			return;
		}
		ctxSet(DefaultMessageFactory.CTXKEY_VAL, performance);
		ctxSet(DefaultMessageFactory.CTXKEY_VAL + "Vctm", victim.getProperties());
		sendHelper().sendLocal(user, null, "force.audience", Category.MAGIC);
		victim.send(getString("force.victimBefore", user.getProperties(), victim.getLang()));
		logger().info(user.getLogin() + " forced " + victim.getLogin() + " to do: " + performance);
		commandDispatcher.process(performance, victim);
		ctxSet(DefaultMessageFactory.CTXKEY_VAL, performance);
		ctxSet(DefaultMessageFactory.CTXKEY_VAL + "Vctm", victim.getProperties());
		if (commandDispatcher.getUserFinder().onlineUsers().contains(victim)) {
			victim.send(getString("force.victimAfter", user.getProperties(), victim.getLang()));
		}
		if (commandDispatcher.getUserFinder().onlineUsers().contains(user)) {
			user.send(getString("force.ok", user.getProperties()));
		}
	}
}
